package com.xjrsoft.module.customerTwo.AppManage.business_opportunity.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjrsoft.core.tool.utils.StringUtil;

import com.xjrsoft.module.base.entity.XjrBaseUser;
import com.xjrsoft.module.base.service.IXjrBaseCodeRuleService;
import com.xjrsoft.module.base.service.IXjrBaseCompanyService;
import com.xjrsoft.module.base.service.IXjrBaseUserService;
import com.xjrsoft.module.customerTwo.AppManage.business_opportunity.entity.BusinessOpportunityLog;


import java.time.LocalDateTime;

import com.xjrsoft.module.customerTwo.AppManage.business_opportunity.dto.BusinessOpportunityLogListDto;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.page.ConventPage;


import java.util.Arrays;
import java.util.List;
import com.xjrsoft.module.customerTwo.AppManage.business_opportunity.mapper.BusinessOpportunityLogMapper;
import com.xjrsoft.module.customerTwo.AppManage.business_opportunity.service.IBusinessOpportunityLogService;
import com.xjrsoft.core.mp.base.BaseService;

import com.xjrsoft.module.customerTwo.barin.t_barin_user_registration.entity.TBarinUser;
import com.xjrsoft.module.customerTwo.barin.t_barin_user_registration.service.ITBarinUserService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 *  服务实现类
 *
 * @author lisen
 * @since 2024-04-25
 */
@Service
@AllArgsConstructor
public class BusinessOpportunityLogServiceImpl extends BaseService<BusinessOpportunityLogMapper, BusinessOpportunityLog> implements IBusinessOpportunityLogService {

	@Resource
	IXjrBaseCodeRuleService codeRuleService;

	@Resource
	IXjrBaseUserService userService;

	@Resource
	ITBarinUserService Tbuserservice;
	@Resource
	IXjrBaseCompanyService companyService;

	@Override
	public IPage<BusinessOpportunityLog> getPageList(BusinessOpportunityLogListDto pageListDto) {
		Wrapper<BusinessOpportunityLog> wrapper = Wrappers.<BusinessOpportunityLog>query().lambda()
				.like(!StringUtil.isEmpty(pageListDto.getLog_number()), BusinessOpportunityLog::getLogNumber, pageListDto.getLog_number())
				.like(!StringUtil.isEmpty(pageListDto.getAssembly_id()), BusinessOpportunityLog::getAssemblyId, pageListDto.getAssembly_id())
				.ge(!StringUtil.isEmpty(pageListDto.getCreate_date_Start()), BusinessOpportunityLog::getCreateDate, pageListDto.getCreate_date_Start())
				.le(!StringUtil.isEmpty(pageListDto.getCreate_date_End()), BusinessOpportunityLog::getCreateDate, pageListDto.getCreate_date_End())
				.like(!StringUtil.isEmpty(pageListDto.getUser_company_id()), BusinessOpportunityLog::getUserCompanyId, pageListDto.getUser_company_id())
				.like(!StringUtil.isEmpty(pageListDto.getContactor()), BusinessOpportunityLog::getContactor, pageListDto.getContactor())
				.ge(!StringUtil.isEmpty(pageListDto.getLast_view_date_Start()), BusinessOpportunityLog::getLastViewDate, pageListDto.getLast_view_date_Start())
				.le(!StringUtil.isEmpty(pageListDto.getLast_view_date_End()), BusinessOpportunityLog::getLastViewDate, pageListDto.getLast_view_date_End())
				.eq(BusinessOpportunityLog::getIslast,1);
		return this.page(ConventPage.getPage(pageListDto), wrapper);
	}

	@Override
	@Transactional
	public Integer addBusinessOpportunityLog(String assemblyId) {
		XjrBaseUser xjrBaseUser = userService.getById(StpUtil.getLoginId().toString());
		TBarinUser tBarinUser = Tbuserservice.getBaseMapper().selectById(xjrBaseUser.getUserId());
		BusinessOpportunityLog lastOne = getLastOne(xjrBaseUser.getUserId(), assemblyId);

		BusinessOpportunityLog businessOpportunityLog = new BusinessOpportunityLog();

		businessOpportunityLog.setUserId(xjrBaseUser.getUserId());

		businessOpportunityLog.setUserCompanyId(tBarinUser.getFFirmname());

		businessOpportunityLog.setContactor(xjrBaseUser.getRealName());

		businessOpportunityLog.setAssemblyId(assemblyId);

		businessOpportunityLog.setLogNumber(codeRuleService.genEncode("BusinessOpportunityCode"));
		businessOpportunityLog.setCreateDate(LocalDateTime.now());
		if(lastOne!=null){
			businessOpportunityLog.setCreateDate(lastOne.getCreateDate());
			lastOne.setIslast(0);
			updateById(lastOne);
		}
		businessOpportunityLog.setLastViewDate(LocalDateTime.now());

		businessOpportunityLog.setIslast(1);
		boolean isSuccess = this.save(businessOpportunityLog);
		return isSuccess ? businessOpportunityLog.getId() : null;
	}

	@Override
	public boolean updateBusinessOpportunityLog(String id, BusinessOpportunityLog businessOpportunityLog) {
		businessOpportunityLog.setId(Integer.valueOf(id));
		return this.updateById(businessOpportunityLog);
	}

	@Override
	public boolean delete(String ids) {
		List<String> idList = Arrays.asList(ids);
		if (this.removeByIds(idList)) {
			return true;
		}
		return false;
	}
	private BusinessOpportunityLog getLastOne(String userId,String assemblyId){
		Wrapper wrapper =  Wrappers.<BusinessOpportunityLog>query().lambda().eq(BusinessOpportunityLog::getUserId, userId).eq(BusinessOpportunityLog::getAssemblyId,assemblyId).eq(BusinessOpportunityLog::getIslast,1);
		return getOne(wrapper);
	}

	@Override
	public BusinessOpportunityLog getByNumber(String number){
		Wrapper wrapper =  Wrappers.<BusinessOpportunityLog>query().lambda().eq(BusinessOpportunityLog::getLogNumber, number);
		return getOne(wrapper);
	}
}